<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use app\admin\controller\Cloudaccountwithdraw;
use think\Db;
use think\Request;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\validate\UserValidate;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class CircleController extends Controller
{

    /**
     * 圈层列表
     * User:@hang
     */
    public function index(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['circle_name']) && !empty($param['circle_name'])) {
            $whehe[] = ['circle_name','like',"%{$param['circle_name']}%"];
        }
        if (isset($param['circle_status']) && !empty($param['circle_status'])) {
            $whehe[] = ['circle_status','=',$param['circle_status']];
        }

        $data = Db::table('fa_circle_list')
            ->where($whehe)
            ->field('*')
            ->order('id asc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $data->each(function ($v, $k) {
            switch($v['circle_status']){
                case '1': $v['circle_status_word'] = '正常';break;
                case '2': $v['circle_status_word'] = '隐藏';break;
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            return $v;
        });
        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }


    //编辑圈层信息
    public function edit($id,Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        if ($request->isPost()) {
            $param = $request->param();
            $Aliuploadimage = New Aliuploadimage();
            //圈层封面
            if (!empty($_FILES['circle_img']['name'])) {
                $file = request()->file('circle_img');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('圈层封面图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['circle_img'] = $applogourlRes['data']['url'];
                }
            }
            //皇冠标志
            if (!empty($_FILES['circle_url']['name'])) {
                $file = request()->file('circle_url');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('皇冠标志图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['circle_url'] = $applogourlRes['data']['url'];
                }
            }
            //皇冠标志
            if (!empty($_FILES['circle_pic']['name'])) {
                $file = request()->file('circle_pic');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('群组头像图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['circle_pic'] = $applogourlRes['data']['url'];
                }
            }
            $IndexTableModel->editInfo('fa_circle_list',"`id` = '{$param['id']}'",$param);
            return admin_success('编辑圈层信息成功');
        }
        $data = $IndexTableModel->getFind("fa_circle_list", "`id` = '{$id}'",'*');
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch();
    }


    /**
     * 圈层用户列表
     * User:@hang
     */
    public function memberindex(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['a.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['a.me_uid','=',$param['me_uid']];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['a.me_phone|a.me_nick_name','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['circle_id']) && !empty($param['circle_id'])) {
            $whehetime = "a.me_circle_id = '{$param['circle_id']}'";
        }
        $whehe[] = ['a.me_circle_str','neq',''];




        $data = Db::table('fa_car_authentication')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_circle_list c",'a.circle_id = c.id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->field('a.*,b.me_uid,b.me_phone,b.me_nick_name,b.me_sex,b.me_circle_sbnum,b.me_is_circle,b.me_car_type,b.me_carname,b.me_circle_id,b.me_circle_url,b.me_circle_str,b.me_is_real,b.me_realname,b.me_realidcard,c.circle_name')
            ->order('a.state asc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $data->each(function ($v, $k) {
            if(empty($v['add_admin_id'])){
                $v['add_admin_id_word'] = '自行认证';
            }else{
                $v['add_admin_id_word'] = '管理端认证';
            }
            switch($v['type']){
                case '1': $v['type_word'] = '私家车辆';break;
                case '2': $v['type_word'] = '企业车辆';break;
                case '3': $v['type_word'] = '运营车辆';break;
            }
            $v['car_money_word'] = $v['car_money'].'万';
            if($v['add_admin_id'] != 0){
                $v['name'] = $v['name'].'(虚拟)';
                $v['car_name'] = $v['car_name'].'(虚拟)';
                $v['car_vin'] = $v['car_vin'].'(虚拟)';
                $v['car_model'] = $v['car_model'].'(虚拟)';
                $v['car_money'] = $v['car_money'].'(虚拟)';
                $v['car_money_word'] = $v['car_money_word'].'(虚拟)';
            }
            $v['add_time'] = date('Y-m-d H:i',$v['add_time']);
            switch($v['is_pass']){
                case '1': $v['is_pass_word'] = '无需审核，直接通过';break;
                case '2': $v['is_pass_word'] = '需要审核';break;
            }
            switch($v['state']){
                case '0': $v['state_word'] = '待审核';break;
                case '1': $v['state_word'] = '已通过';break;
                case '2': $v['state_word'] = '已拒绝';break;
            }
            return $v;
        });
        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }


}
